package com.sxhalo.fsms.controller.api;

import com.sxhalo.fsms.controller.BaseController;
import com.sxhalo.fsms.domain.FactDeviceLinkage;
import com.sxhalo.fsms.domain.FactDeviceLinkageCommand;
import com.sxhalo.fsms.domain.FactDeviceLinkageRule;
import com.sxhalo.fsms.log.SysControllerLog;

import com.sxhalo.fsms.service.IFactDeviceLinkageCommandService;
import com.sxhalo.fsms.service.IFactDeviceLinkageRuleService;
import com.sxhalo.fsms.service.IFactDeviceLinkageService;
import com.sxhalo.fsms.util.ApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;

@Controller
@RequestMapping("/api")
public class DeviceLinkageController extends BaseController {

	@Autowired
	private IFactDeviceLinkageService linkageService;
	@Autowired
	private IFactDeviceLinkageCommandService linkageCommandService;
	@Autowired
	private IFactDeviceLinkageRuleService linkageRulesService;

	@ResponseBody
	@RequestMapping(value = "/device/linkage", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	public ApiResult findDevicelinkage(Integer current, Integer pageSize, HttpServletRequest req) {
		String where = "delete_flag=0";
		String order = "";
		if (!isBlank(req.getParameter("name"))) {
			where += " and name like '%" + req.getParameter("name").trim() + "%'";
		}
		if (!isBlank(req.getParameter("isSuspend"))) {
			where += " and is_suspend = " + req.getParameter("isSuspend").trim();
		}
		if (!isBlank(req.getParameter("useState"))) {
			where += " and use_state = " + req.getParameter("useState").trim() ;
		}

		return findList(current, pageSize, where, order, linkageService);
	}

	@ResponseBody
	@SysControllerLog(operationName = "新增联动任务", modelName = "联动任务")
	@RequestMapping(value = "/device/linkage", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ApiResult saveDevicelinkage(@RequestBody FactDeviceLinkage record, HttpServletRequest req) {
		Timestamp nowtime = new Timestamp(System.currentTimeMillis());
		record.setUpdateTime(nowtime);
		record.setDeleteFlag(0);
		record.setCreateTime(nowtime);
		linkageService.insertSelective(record);
		return ApiResult.retOK(req,record.getId());
	}

	@ResponseBody
	@SysControllerLog(operationName = "编辑联动任务", modelName = "联动任务")
	@RequestMapping(value = "/device/linkage", method = RequestMethod.PUT, produces = "application/json;charset=UTF-8")
	public ApiResult updateDevicelinkage(@RequestBody FactDeviceLinkage record, HttpServletRequest req) {
		Timestamp nowtime = new Timestamp(System.currentTimeMillis());
		record.setUpdateTime(nowtime);
		linkageService.updateByPrimaryKeySelective(record);
		return ApiResult.retOK(req);
	}

	@ResponseBody
	@SysControllerLog(operationName = "删除联动任务", modelName = "联动任务")
	@RequestMapping(value = "/device/linkage", method = RequestMethod.DELETE, produces = "application/json;charset=UTF-8")
	public ApiResult delDevicelinkage(Integer id, HttpServletRequest req) {
		FactDeviceLinkage record = new FactDeviceLinkage();
		record.setId(id);
		record.setDeleteFlag(1);
		linkageService.updateByPrimaryKeySelective(record);
		return ApiResult.retOK(req);
	}


	@ResponseBody
	@SysControllerLog(operationName = "批量删除", modelName = "设备联动条件配置")
	@RequestMapping(value = "/device/linkage/batchdel", method = RequestMethod.DELETE, produces = "application/json;charset=UTF-8")
	public ApiResult batchDelDevicelinkageRule(String  ids, String method, HttpServletRequest req) {
		linkageService.deleteByWhere(" id in ("+ids+")");
		return ApiResult.retOK(req);
	}






	@ResponseBody
	@RequestMapping(value = "/device/linkage/rule", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	public ApiResult findDevicelinkageRule(Integer current, Integer pageSize, Integer id,HttpServletRequest req) {
		String where = "a.delete_flag=0"+(id==null?"":" and linkage_id="+id);
		String order = "";

		return findList(current, pageSize, where, order, linkageRulesService);
	}


	@ResponseBody
	@SysControllerLog(operationName = "新增", modelName = "设备联动条件配置")
	@RequestMapping(value = "/device/linkage/rule", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ApiResult saveDevicelinkageRule( FactDeviceLinkageRule record, String  ids, HttpServletRequest req) {
//		Timestamp nowtime = new Timestamp(System.currentTimeMillis());
//		record.setUpdateTime(nowtime);
//		record.setCreateTime(nowtime);
//		linkageRulesService.insertSelective(record);
		if(record.getSymbol()==null){
			record.setSymbol("=");
		}

		linkageRulesService.batchInsert(record,ids);
		return ApiResult.retOK(req);
	}

	@ResponseBody
	@SysControllerLog(operationName = "编辑", modelName = "设备联动条件配置")
	@RequestMapping(value = "/device/linkage/rule", method = RequestMethod.PUT, produces = "application/json;charset=UTF-8")
	public ApiResult updateDevicelinkageRule(@RequestBody FactDeviceLinkageRule record, HttpServletRequest req) {
		Timestamp nowtime = new Timestamp(System.currentTimeMillis());
		record.setUpdateTime(nowtime);
		linkageRulesService.updateByPrimaryKeySelective(record);
		return ApiResult.retOK(req);
	}

	@ResponseBody
	@SysControllerLog(operationName = "删除", modelName = "设备联动条件配置")
	@RequestMapping(value = "/device/linkage/rule", method = RequestMethod.DELETE, produces = "application/json;charset=UTF-8")
	public ApiResult delDevicelinkageRule(Integer id, HttpServletRequest req) {
		FactDeviceLinkageRule record = new FactDeviceLinkageRule();
		record.setId(id);
		record.setDeleteFlag(1);
		linkageRulesService.updateByPrimaryKeySelective(record);
		return ApiResult.retOK(req);
	}





	@ResponseBody
	@RequestMapping(value = "/device/linkage/command", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	public ApiResult findDevicelinkageCommand(Integer current, Integer pageSize,Integer id, HttpServletRequest req) {
		String where = "a.delete_flag=0"+(id==null?"":" and linkage_id="+id);
		String order = "";

		return findList(current, pageSize, where, order, linkageCommandService);
	}


	@ResponseBody
	@SysControllerLog(operationName = "新增", modelName = "联动任务动作配置")
	@RequestMapping(value = "/device/linkage/command", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ApiResult saveDevicelinkageCommand( FactDeviceLinkageCommand record, String ids, HttpServletRequest req) {
//		Timestamp nowtime = new Timestamp(System.currentTimeMillis());
//		record.setUpdateTime(nowtime);
//		record.setCreateTime(nowtime);
//		linkageCommandService.insertSelective(reidscord);
		linkageCommandService.batchInsert(record,ids);
		return ApiResult.retOK(req);
	}

	@ResponseBody
	@SysControllerLog(operationName = "编辑", modelName = "联动任务动作配置")
	@RequestMapping(value = "/device/linkage/command", method = RequestMethod.PUT, produces = "application/json;charset=UTF-8")
	public ApiResult updateDevicelinkageCommand(@RequestBody FactDeviceLinkageCommand record, HttpServletRequest req) {
		Timestamp nowtime = new Timestamp(System.currentTimeMillis());
		record.setUpdateTime(nowtime);
		linkageCommandService.updateByPrimaryKeySelective(record);
		return ApiResult.retOK(req);
	}

	@ResponseBody
	@SysControllerLog(operationName = "删除", modelName = "联动任务动作配置")
	@RequestMapping(value = "/device/linkage/command", method = RequestMethod.DELETE, produces = "application/json;charset=UTF-8")
	public ApiResult delDevicelinkageCommand(Integer id, HttpServletRequest req) {
		FactDeviceLinkageCommand record = new FactDeviceLinkageCommand();
		record.setId(id);
		record.setDeleteFlag(1);
		linkageCommandService.updateByPrimaryKeySelective(record);
		return ApiResult.retOK(req);
	}

}
